package py.una.pol.paralelos;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Graph {

	private int numVertexs;
	private Vertex V[];
	private int W[][];
	private int r;

	public void loadGraph(String filePath) {
		BufferedReader br = null;

		try {

			String line;
			br = new BufferedReader(new FileReader(filePath));
			line = br.readLine();
			if (line.contains("//Number vertexs")) {
				line = br.readLine();
				this.numVertexs = Integer.parseInt(line);
			} else {
				throw new RuntimeException("Number vertexs not found");
			}

			line = br.readLine();
			if (line.contains("//Vertexs labels")) {
				this.V = new Vertex[numVertexs];
				for (int i = 0; i < this.numVertexs; i++) {
					line = br.readLine();
					this.V[i] = new Vertex(line);
				}
			} else {
				throw new RuntimeException("Vertexs labels not found");
			}

			line = br.readLine();
			if (line.contains("//Adjancecy Matrix")) {
				this.W = new int[this.numVertexs][this.numVertexs];
				for (int i = 0; i < this.numVertexs; i++) {
					for (int j = 0; j < this.numVertexs; j++) {
						line = br.readLine();
						this.W[i][j] = Integer.parseInt(line);
					}
				}
			} else {
				throw new RuntimeException("Adjancecy Matrix not found");
			}

			line = br.readLine();
			if (line.contains("//root")) {
				line = br.readLine();
				r = Integer.parseInt(line);
			} else {
				throw new RuntimeException("Root not found");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public void printNumberVertexs() {
		System.out.println("Number vertexs:\t" + this.numVertexs);
	}

	public void printVertexs() {
		System.out.println("Vertexs: ");
		for (int i = 0; i < this.numVertexs; i++) {
			System.out.println("\t" + this.V[i].getLabel());
		}
	}

	public void printAdjacencyMatrix() {
		System.out.println("AdjacencyMatrix: ");
		for (int i = 0; i < this.numVertexs; i++) {
			System.out.println();
			for (int j = 0; j < this.numVertexs; j++) {
				System.out.print("\t" + this.W[i][j]);
			}
		}
	}

	public void printRoot() {
		System.out.println("Root:\t" + this.r);
	}

	public void printConfiguration() {
		System.out.println();
		this.printNumberVertexs();
		System.out.println();
		this.printVertexs();
		System.out.println();
		this.printAdjacencyMatrix();
		System.out.println();
		this.printRoot();
	}

	public int getW(int i, int j) {
		return this.W[i][j];
	}

	public int getNumVertexs() {
		return numVertexs;
	}

	public void setNumVertexs(int numVertexs) {
		this.numVertexs = numVertexs;
	}

	public Vertex getV(int i) {
		return V[i];
	}

	public Vertex[] getV() {
		return V;
	}

	public void setV(Vertex[] v) {
		V = v;
	}

	public int[][] getW() {
		return W;
	}

	public void setW(int[][] w) {
		W = w;
	}

	public int getR() {
		return r;
	}

	public void setR(int r) {
		this.r = r;
	}

}
